

#import "FAMMineViewController.h"
#import "FAMMineTableTopViewCell.h"
#import "FAMMineTableHeaderView.h"
#import "FAMMineTablebottomViewCell.h"
#import "FAMMineTableFooterView.h"
@interface FAMMineViewController ()<UITableViewDelegate,UITableViewDataSource>
@property (weak, nonatomic) IBOutlet UITableView *FAMTableView;
@property (nonatomic, strong)FAMMineTableHeaderView *FAMTableHeaderView;
@property (nonatomic, strong)FAMMineTableFooterView *FAMTableFooterView;
@end

@implementation FAMMineViewController
- (void)viewWillAppear:(BOOL)animated{
    [super viewWillAppear:animated];
    [self FAM_loadData];
}
- (void)viewDidLoad {
    [super viewDidLoad];
    [self FAM_showUi];
    
}
- (void)FAM_showUi{
    self.fd_prefersNavigationBarHidden = YES;
    self.FAMTableView.contentInsetAdjustmentBehavior = UIScrollViewContentInsetAdjustmentNever;
    [self.FAMTableView registerNib:[UINib nibWithNibName:@"FAMMineTableTopViewCell" bundle:nil] forCellReuseIdentifier:@"topcell"];
    [self.FAMTableView registerNib:[UINib nibWithNibName:@"FAMMineTablebottomViewCell" bundle:nil] forCellReuseIdentifier:@"bottomcell"];
    self.FAMTableView.separatorStyle = UITableViewCellSeparatorStyleNone;
    
    self.FAMTableHeaderView = [[FAMMineTableHeaderView alloc]initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH, 400)];
    UIView *tempHeaderView = [[UIView alloc]initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH, 400)];
    [tempHeaderView addSubview:self.FAMTableHeaderView];
    self.FAMTableView.tableHeaderView = tempHeaderView;
    
    self.FAMTableFooterView = [[FAMMineTableFooterView alloc]initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH, 350)];
    UIView *tempFooterView = [[UIView alloc]initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH, 350)];
    tempFooterView.backgroundColor = kCustom0xColor(0xF7F8F9, 1.0);
    [tempFooterView addSubview:self.FAMTableFooterView];
    self.FAMTableView.tableFooterView = tempFooterView;
    
}
- (void)FAM_loadData{
    WeakSelf(self);
//    [self showHudInKeyWindowWithHint:nil];
    [FAMHttpTool FAM_GetFoodmateUserdata:^(FAMGetFoodmateUserdataModel * _Nonnull model, NSString * _Nonnull msg) {
        NSDate *currentDate = [NSDate date];
        NSCalendar *calendar = [NSCalendar currentCalendar];
        NSDate *sevenDaysAgo = [calendar dateByAddingUnit:NSCalendarUnitDay value:-7 toDate:currentDate options:NSCalendarWrapComponents];
        NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
        [dateFormatter setDateFormat:@"yyyyMMdd"];
        NSString *begin_date = [dateFormatter stringFromDate:sevenDaysAgo];
        NSString *end_date = [FAMTool FAM_getYearWithMonthWithDay];
        [FAMHttpTool FAM_GetFoodmateStatisticsWithData:[NSString stringWithFormat:@"%@/%@",begin_date,end_date] success:^(FAMFoodmateStatisticsModel * _Nonnull model, NSString * _Nonnull msg) {
            dispatch_async(dispatch_get_main_queue(), ^{
                weakSelf.FAMTableHeaderView.FAMStatisticsModel = model;
            });
        } failure:^(NSString * _Nonnull errorInfo) {
            dispatch_async(dispatch_get_main_queue(), ^{
                [weakSelf showHint:errorInfo];
            });
        }];
        dispatch_async(dispatch_get_main_queue(), ^{
//            [weakSelf hideHudKeyWindow];
            [weakSelf FAM_fillUi:model];
        });
    } failure:^(NSString * _Nonnull errorInfo) {
        dispatch_async(dispatch_get_main_queue(), ^{
//            [weakSelf hideHudKeyWindow];
            [weakSelf showHint:errorInfo];
        });
    }];
}
- (void)FAM_fillUi:(FAMGetFoodmateUserdataModel *)model{
    self.FAMTableHeaderView.FAMUserModel = model;
    [FAMUserManger shareInstance].userModel = model;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    return 2;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    if (indexPath.row == 0) {
        FAMMineTableTopViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"topcell" forIndexPath:indexPath];
        return cell;
    }else{
        FAMMineTablebottomViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"bottomcell" forIndexPath:indexPath];
        return cell;
    }
}
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    if (indexPath.row == 0) {
        return 160;
    }else{
        return 240;
    }
}
@end
